package mapreduce;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;
import java.util.*;

public class FlowCountReduce extends Reducer<Text ,PhoneFlow, PhoneFlow ,Text> {

    @Override
    protected void reduce(Text key, Iterable<PhoneFlow> values, Reducer<Text,PhoneFlow, PhoneFlow,Text>.Context context) throws IOException, InterruptedException {

          PhoneFlow phoneFlow = new PhoneFlow();
          phoneFlow.set(key.toString(),new Long(0),new Long(0),new Long(0));

          for (PhoneFlow value : values) {
            phoneFlow.setUpFlow(phoneFlow.getUpFlow()+value.getUpFlow());
            phoneFlow.setDownFlow(phoneFlow.getDownFlow()+value.getDownFlow());
            phoneFlow.setFlowSum(phoneFlow.getUpFlow()+phoneFlow.getDownFlow());
          }
          //将结果输出
          context.write(phoneFlow,new Text(""));
    }

}